﻿''' <summary>
''' This class implements an event log collector.
''' </summary>
''' <remarks>
''' The purpose of the event log collector is to retrieve event log entries
''' from a (local or remote) event log and include them in the system info
''' report.
''' </remarks>
Public Class EventLogCollector
    Implements ICollector

    ''' <summary>
    ''' List of writers passed when Startup() is called.
    ''' </summary>
    ''' <remarks></remarks>
    Protected m_writers As List(Of Writer.IWriter)

    ''' <summary>
    ''' List of event log entries.
    ''' </summary>
    ''' <remarks></remarks>
    Protected m_entries As List(Of EventLogEntry)

    ''' <summary>
    ''' This holds an error description.
    ''' </summary>
    ''' <remarks></remarks>
    Protected m_error As String

    Public Sub Cleanup() Implements ICollector.Cleanup

        ' Nothing to do

    End Sub

    Public Sub Run() Implements ICollector.Run

        'Write our category name.
        Writer.WritersHelper.WriteCategory("Event Log Information", m_writers)

        'If an error occurred while reading the configuration file, write
        'it out and stop here.
        If m_error <> "" Then
            Writer.WritersHelper.WriteCategoryError(m_error, m_writers)
        Else
            'Scan all event log entries.
            For Each ev As EventLogEntry In m_entries
                Try
                    'Write the event log information.
                    If ev.isLocalMachine Then
                        Writer.WritersHelper.WriteSection(ev.Name, m_writers)
                    Else
                        Writer.WritersHelper.WriteSection(ev.Name + ", " + ev.Machine, m_writers)
                    End If

                    'Process this item.
                    Try
                        'Does it exist?
                        If System.Diagnostics.EventLog.Exists(ev.Name, ev.getMachineName) Then
                            'Get an instance.
                            Dim log As New EventLog(ev.Name, ev.getMachineName)
                            Try
                                'Loop to get the items we want.
                                Dim count As Integer = 0, index As Integer = log.Entries.Count - 1
                                While (count < ev.Number) AndAlso (index >= 0)
                                    If ev.isTypeIncluded(log.Entries(index).EntryType.ToString()) Then
                                        count += 1
                                        Writer.WritersHelper.WriteEntry("Date/Time", log.Entries(index).TimeWritten.ToString(), m_writers)
                                        Writer.WritersHelper.WriteEntry("Type", log.Entries(index).EntryType.ToString(), m_writers)
                                        Writer.WritersHelper.WriteEntry("Source", log.Entries(index).Source, m_writers)
                                        Writer.WritersHelper.WriteEntry("Category", log.Entries(index).Category, m_writers)
                                        Writer.WritersHelper.WriteEntry("User", log.Entries(index).UserName, m_writers)
                                        Writer.WritersHelper.WriteEntry("Text", log.Entries(index).Message, m_writers)
                                        Writer.WritersHelper.WriteBlankEntry(m_writers)
                                    End If
                                    index -= 1
                                End While
                            Catch ex As Exception
                                'Error.
                                Writer.WritersHelper.WriteEntryError("Error retrieving information for eveng log (" + ex.Message + ")", m_writers)
                            End Try
                        Else
                            'Does not exist.
                            Writer.WritersHelper.WriteSectionError("Log does not exist", m_writers)
                        End If

                    Catch ex As Exception
                        'Error gathering info for this one.
                        Writer.WritersHelper.WriteSectionError("Error retrieving information for event log (" + ex.Message + ")", m_writers)
                    End Try
                Catch ex As Exception
                    'Error gathering info.
                    Writer.WritersHelper.WriteSectionError("Error retrieving event logs information (" + ex.Message + ")", m_writers)
                End Try

                'Write out a blank.
                Writer.WritersHelper.WriteBlank(m_writers)
            Next
        End If

    End Sub

    Public Sub Startup(ByVal cfgFile As String, ByVal writers As System.Collections.Generic.List(Of Writer.IWriter)) Implements ICollector.Startup

        'Get all writers.
        m_writers = writers
        'Read configuration file.
        ReadConfiguration(cfgFile)

    End Sub

    ''' <summary>
    ''' Read the configuration file.
    ''' </summary>
    ''' <param name="fName">File name with event log collector configuration.</param>
    ''' <remarks></remarks>
    Protected Sub ReadConfiguration(ByVal fName As String)

        Const EVENTLOG_TABLE As String = "EventLog"

        Try
            'Read in a dataset.
            Dim ds As New DataSet
            ds.ReadXml(fName)
            m_entries = New List(Of EventLogEntry)

            'Parse all entries.
            For i As Integer = 0 To ds.Tables(EVENTLOG_TABLE).Rows.Count - 1
                With ds.Tables(EVENTLOG_TABLE).Rows(i)
                    m_entries.Add(New EventLogEntry(Convert.ToString(.Item("Machine")), _
                                                    Convert.ToString(.Item("Name")), _
                                                    Convert.ToString(.Item("Type")), _
                                                    Convert.ToString(.Item("Number"))))
                End With
            Next

            ds.Dispose()
            ds = Nothing

            'Signal no error.
            m_error = ""
        Catch ex As Exception
            'Keep the error here.
            m_error = ex.ToString()
        End Try

    End Sub

End Class
